package com.beta.cat.config.sms;

import com.alibaba.fastjson.JSONObject;
import com.beta.cat.utils.AESUtil;
import com.beta.cat.utils.JsonUtil;
import com.beta.cat.utils.Md5Utils;
import com.beta.cat.utils.RequestParamUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.UUID;

@Slf4j
public class BetaSmsVerifyCodeService implements SmsVerifyService {
    /**
     * 当前组件的产品名称
     */
    public static final String PRODUCT_NAME = "beta";
    @Resource
    RestTemplate restTemplate;
    @Resource
    SmsVerifyCodeAutoConfiguration.SmsVerifyProperties properties;

    @Override
    public SmsResult sendVerifyCode(String mobile) {
        //获取当前时间戳，格式：DateTime.Now.Ticks.ToString()
        long timestamp = (System.currentTimeMillis() + 8 * 3600 * 1000) * 10000 + 621355968000000000L;
        String validationCode = Md5Utils.stringToMD5(properties.getPrivateKey() + timestamp);
        String key = URLEncoder.encode(String.format(properties.getSecretKey(), timestamp, UUID.randomUUID(), validationCode));
        String param = AESUtil.encrypt(String.format(properties.getStrSetting("sendParam"), mobile, RequestParamUtil.getIpAdress(), key), properties.getStrSetting("serverAesKey"));
        log.info("getSms param:{}", String.format(properties.getStrSetting("sendParam"), mobile, RequestParamUtil.getIpAdress(), key), properties.getStrSetting("serverAesKey"));

        try {
            String responseStr = restTemplate.getForObject(properties.getServerUrl() + String.format(properties.getStrSetting("smsPath"), param), String.class);
            log.info("getSms responseStr:{}", responseStr);
            SmsResult smsResultVO = JsonUtil.parseObject(responseStr, SmsResult.class);
            log.info("getSms authResultVO{}", JSONObject.toJSONString(smsResultVO));
            return smsResultVO;
        } catch (Exception e) {
            log.error("获取验证码失败,mobile:{}", mobile, e);
            SmsResult ret = new SmsResult();
            ret.setSuccess(false);
            ret.setMessage("获取验证码失败");
            return ret;
        }
    }

    @Override
    public SmsResult verifyCode(String mobile, String verifyCode) {
        //获取当前时间戳，格式：DateTime.Now.Ticks.ToString()
        long timestamp = (System.currentTimeMillis() + 8 * 3600 * 1000) * 10000 + 621355968000000000L;
        String validationCode = Md5Utils.stringToMD5(properties.getPrivateKey() + timestamp);
        String key = URLEncoder.encode(String.format(properties.getSecretKey(), timestamp, UUID.randomUUID(), validationCode));
        String param = AESUtil.encrypt(String.format(properties.getStrSetting("verifyParam"), mobile, verifyCode, RequestParamUtil.getIpAdress(), key), properties.getStrSetting("serverAesKey"));
        log.info("getSms param:{}", String.format(properties.getStrSetting("verifyParam"), mobile, verifyCode, RequestParamUtil.getIpAdress(), key), properties.getStrSetting("serverAesKey"));

        try {
            String responseStr = restTemplate.getForObject(properties.getServerUrl() + String.format(properties.getStrSetting("smsPath"), param), String.class);
            log.info("getSms responseStr:{}", responseStr);
            SmsResult smsResultVO = JsonUtil.parseObject(responseStr, SmsResult.class);
            log.info("getSms authResultVO{}", JSONObject.toJSONString(smsResultVO));
            return smsResultVO;
        } catch (Exception e) {
            log.error("校验验证码失败,mobile:{}", mobile, e);
            SmsResult ret = new SmsResult();
            ret.setSuccess(false);
            ret.setMessage("校验验证码失败");
            return ret;
        }
    }
}
